from collections import defaultdict
from typing import List


def serialize_project_base_info(project: 'Project'):
    return {
        'id': project.id,
        'status': project.status,
        'start_at': project.start_at,
        'end_at': project.end_at,
        'update_at': project.update_at,
        'name': project.name,
    }


def serialize_project_all_info(project: 'Project'):
    data = serialize_project_base_info(project)
    data['skills'] = project.need_skills
    data['backend'] = project.backend
    return data


def serialize_process(process: 'Process'):
    return {
        'id': process.id,
        'status': process.status,
        'start_at': process.start_at,
        'end_at': process.end_at,
        'update_at': process.update_at,
        'name': process.name,
    }


def serialize_checklist(checklist: 'CheckList', user: 'User' = None):
    from apps.users.serializers import serialize_user
    user_data = serialize_user(user) if user else None
    return {
        'id': checklist.id,
        'name': checklist.name,
        'status': checklist.status,
        'type': checklist.type,
        'start_at': checklist.start_at,
        'end_at': checklist.end_at,
        'update_at': checklist.update_at,
        'user': user_data
    }


def serialize_checklist_group_by_type(checklists: List['CheckList']):
    result = defaultdict(list)
    for checklist in checklists:
        result[checklist.type].append(serialize_checklist(checklist))
    return result
